package com.thermal.thermal_inspection.service.impl;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.*;
import com.thermal.thermal_inspection.mapper.*;
import com.thermal.thermal_inspection.service.BatchService;
import com.thermal.thermal_inspection.vo.BatchDetailVo;
import com.thermal.thermal_inspection.vo.BatchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.graphql.GraphQlProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.util.ArrayList;
import java.util.List;

@Service
public class BatchServiceImpl implements BatchService {
    @Autowired
    private BatchMapper batchMapper;
    @Autowired
    private InspectionDetailMapper inspectionDetailMapper;
    @Autowired
    private InspectionWorkMapper inspectionWorkMapper;
    @Autowired
    private InspectionValueMapper inspectionValueMapper;
    @Autowired
    private InspectionDataMapper inspectionDataMapper;
    @Autowired
    private InspectionGroupMapper inspectionGroupMapper;
    @Autowired
    private MapDetailMapper mapDetailMapper;
    /**
     * 添加批次
     * @param batch
     * @return Integer
     */
    @Override
    public Integer addBatch(Batch batch) {
        return batchMapper.addBatch(batch);
    }

    /**
     * 获取所有批次
     * @return
     */
    @Override
    public List<Batch> getBatchs() {
        return batchMapper.getBatchs();
    }

    /**
     * 是否存在这个批次名
     * @param batchName
     * @return boolean
     */
    @Override
    public boolean existBatchName(String batchName) {
        Batch batch = batchMapper.getBatchByName(batchName);
        if (batch == null)
            return false;
        return true;
    }

    /**
     * 通过批次Id
     * @param batchId
     * @return Batch
     */
    @Override
    public Batch getBatchById(int batchId) {
        return batchMapper.getBatchById(batchId);
    }

    /**
     * 获取临时任务批次
     * @return List<Batch>
     */
    @Override
    public List<Batch> getBatchOfNoBatch() {
        return batchMapper.getBatchOfNoBatch();
    }

    /**
     * 修改batch信息
     * @param batchVO
     * @return Integer
     */
    @Override
    public Integer updateBatch(BatchVO batchVO) {
        return batchMapper.updateBatch(batchVO.getBatchId(), batchVO.getNewBatchName());
    }

    /**
     * 删除批次信息
     * @param batchId
     * @return Integer
     */
    @Override
    public Integer deleteBatch(int batchId) {
        Batch batch = batchMapper.getBatchById(batchId);
        int row = 1;
        row = batchMapper.deleteBatch(batchId);
        System.out.println(row);
        if (row == 0)
            return 0;
        for (int i = batch.getWorkBeginId();i<=batch.getWorkEndId();i++){
            InspectionWork inspectionWork = inspectionWorkMapper.getInspectionWorkById(i);
            //先保证存在inspectionWork
            if (inspectionWork != null){
                inspectionWorkMapper.deleteInspectionWork(i);
                List<InspectionDetail> inspectionDetails = inspectionDetailMapper.getDetailByWorkId(i);
                for (InspectionDetail inspectionDetail : inspectionDetails){
                    inspectionValueMapper.deleteInspectionValue(inspectionDetail.getInspectionDetailId());
                }
                inspectionDetailMapper.deleteInspectionWork(i);
            }
        }
        return 1;
    }

    /**
     * 获取批次的模版信息
     * @param batchId
     * @return
     */
    @Override
    public List<BatchDetailVo> getInspectionDataByBatch(int batchId) {
        Batch batch = batchMapper.getBatchById(batchId);
        InspectionWork inspectionWork = null;
        for (int i =batch.getWorkBeginId() ; i <= batch.getWorkEndId();i++){
            inspectionWork = inspectionWorkMapper.getInspectionWorkById(i);
            if (inspectionWork != null)
                break;
        }
        if (inspectionWork == null)
            return null;
        List<InspectionData> inspectionDatas = inspectionDataMapper.getInspectionDataListById(inspectionWork.getInspectionId());
        List<BatchDetailVo> batchDetailVos = new ArrayList<>();
        for (InspectionData inspectionData : inspectionDatas){
            String mapDetailDesc = mapDetailMapper.getMapDetailById(inspectionData.getMapDetailId()).getMapDetailDesc();
            BatchDetailVo batchDetailVo = new BatchDetailVo(inspectionData,mapDetailDesc);
            batchDetailVos.add(batchDetailVo);
        }
        return batchDetailVos;
    }

    /**
     * 修改批次的未开始巡检点位任务信息
     */
    @Override
    public String updateInspectionDetailOfBatch(int batchId, List<InspectionDetail> inspectionDetails) {
        Batch batch = batchMapper.getBatchById(batchId);
        if (batch == null)
            return "ERROR";
        for (int i = batch.getWorkBeginId() ; i <=batch.getWorkEndId();i++){
            InspectionWork inspectionWork = inspectionWorkMapper.getInspectionWorkById(i);
            if (inspectionWork != null && inspectionWork.getInspectionWorkStatus() == 4){
                for (InspectionDetail inspectionDetail : inspectionDetails){
                    inspectionDetail.setInspectionWorkId(i);
                    System.out.println(i+":"+inspectionDetail.getInspectionDataId());
                    if (inspectionGroupMapper.getInspectionGroupByInspectionId(inspectionDetail.getInspectionGroupId()) == null)
                        return "ABNORMAL";
                    inspectionDetailMapper.updateInspectionDetailByDataIdAndWorkId(inspectionDetail);
                }
            }
        }
        return "SUCCESS";
    }
}
